package WebPAC::Output::MARC;

use warnings;
use strict;

use base qw/WebPAC::Common/;

use MARC::Record 2.0;	# need 2.0 for utf-8 encoding see marcpm.sf.net
use MARC::Lint;
use Data::Dump qw/dump/;
use Encode qw/from_to decode/;

=head1 NAME

WebPAC::Output::MARC - Create MARC records from C<marc_*> normalisation rules

=head1 VERSION

Version 0.04

=cut

our $VERSION = '0.04';

=head1 SYNOPSIS

Create MARC records from C<marc_*> normalisation rules described in
L<WebPAC::Normalize>.


=head1 FUNCTIONS

=head2 new

  my $marc = new WebPAC::Output::MARC(
  	path => '/path/to/output.marc',
	native_encoding => 'iso-8859-2',
	marc_encoding => 'utf-8',
	lint => 1,
	dump => 0,
  )

=cut

sub new {
	my $class = shift;
	my $self = {@_};
	bless($self, $class);

	my $log = $self->_get_logger;

	if ($self->{lint}) {
		$self->{lint}= new MARC::Lint or
			$log->warn("Can't create MARC::Lint object, linting is disabled");
	}

	if (my $path = $self->{path}) {
		open($self->{fh}, '>', $path) ||
			$log->logdie("can't open MARC output $path: $!");
		binmode($self->{fh}, ':utf8');

		$log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n");
	} else {
		$log->logconfess("new called without path");
	}

	$self->{native_encoding} ||= 'iso-8859-2';
	$self->{marc_encoding} ||= 'utf-8';

	$self ? return $self : return undef;
}

=head2 add

  $marc->add(
	id => $mfn,
	fields => WebPAC::Normalize::_get_marc_fields(),
	leader => WebPAC::Normalize::marc_leader(),
	row => $row,
  );

C<row> is optional parametar which is used when dumping original row to
error log.

=cut

sub add {
	my $self = shift;

	my $arg = {@_};

	my $log = $self->_get_logger;

	$log->logconfess("add needs fields and id arguments")
		unless ($arg->{fields} && defined $arg->{id});

	my $marc = new MARC::Record;
	$marc->encoding( $self->{marc_encoding} );

	my $id = $arg->{id};

	$log->logconfess("fields isn't array") unless (ref($arg->{fields}) eq 'ARRAY');

	my $fields = $arg->{fields};

	$log->debug("original fields = ", sub { dump( $fields ) });

	# recode fields to marc_encoding
	foreach my $j ( 0 .. $#$fields ) {
		foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) {
			my $f = $fields->[$j]->[ ($i * 2) + 4 ];
			$f = decode( $self->{native_encoding}, $f );
			$fields->[$j]->[ ($i * 2) + 4 ] = $f;
		}
	}

	# sort fields
	@$fields = sort { $a->[0] <=> $b->[0] } @$fields;

	$log->debug("recode fields = ", sub { dump( $fields ) });

	$marc->add_fields( @$fields );

	# tweak leader
	if (my $new_l = $arg->{leader}) {

		my $leader = $marc->leader;

		foreach my $o ( keys %$new_l ) {
			my $insert = $new_l->{$o};
			$leader = substr($leader, 0, $o) .
				$insert . substr($leader, $o+length($insert));
		}
		$marc->leader( $leader );
	}

	if ($self->{lint}) {
		$self->{lint}->check_record( $marc );
		my @w = $self->{lint}->warnings;
		if (@w) {
			$log->error("MARC lint detected warning on record $id\n",
				"<<<<< Original input row:\n",dump($arg->{row}), "\n",
				">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
				"!!!!! MARC lint warnings:\n",join("\n",@w),"\n"
			);
			map { $self->{_marc_lint_warnings}->{$_}++ } @w;
		}
	}

	if ($self->{dump}) {
		$log->info("MARC record on record $id\n",
			"<<<<< Original imput row:\n",dump($arg->{row}), "\n",
			">>>>> Normalized MARC row: leader: [", $marc->leader(), "]\n", dump( $fields ), "\n",
		);
	}

	print {$self->{fh}} $marc->as_usmarc;

}

=head2 finish

Close MARC output file

  $marc->finish;

It will also dump MARC lint warnings summary if called with C<lint>.

=cut

sub finish {
	my $self = shift;

	my $log = $self->get_logger;

	close( $self->{fh} ) or $log->logdie("can't close ", $self->{path}, ": $!");

	if (my $w = $self->{_marc_lint_warnings}) {
		$log->error("MARC lint warnings summary:\n",
			join ("\n",
				map { $w->{$_} . "\t" . $_ }
				sort { $w->{$b} <=> $w->{$a} } keys %$w
			)
		);
	}
}

=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of WebPAC::Output::MARC
